var sysUserList = {};
var sysRoleList = {};//存储用户角色
var SysUserList = function () {
    this.init = function () {
        //实例化表格
        layuiFunciton.tableInit({
            tableId: 'sysUserTalbe',
            url: layuiFunciton.URL + '/sysUser/findUserList',
            id: 'sysUserId',
            searchFromId: 'sysUserSearchForm',
            tableFilter: 'sysUserFilter',  //表格监听名称,
            colsTf: true,
            //limit:3,
            cols: [[
                {type: 'numbers', title: '序号', fixed: 'left'},
                {field: 'account', title: '登陆账号'},
                {field: 'name', title: '真实姓名'},
                {field: 'lastLoginIp', title: '登录IP'},
                {field: 'roleNames', title: '使用角色'},
                {
                    field: 'unionName', title: '所属组织', templet: function (data) {
                    var html = '-';
                    if (data.unionName) {
                        html = data.unionName;
                    }
                    return html;
                }
                },
                {
                    field: 'enabled', title: '用户状态', width: 110, sort: true, templet: function (data) {
                    var html = '<input type="checkbox" name="enabled" value="' + data.id + '" lay-skin="switch" lay-text="启用|禁用" lay-filter="enabledDemo" ';
                    if (data.enabled == 1) {
                        html += 'checked';
                    }
                    html += '>';
                    return html;
                }
                }
                , {field: 'lastLoginTime', title: '登录时间', sort: true}
                , {
                    title: '操作',
                    width: 160,
                    align: "center",
                    templet: '#voucherBar',
                    fixed: 'right',
                    templet: function (row) {
                        return '<a href="javascript:void(0)" style="color:#009feb;" lay-event="resetPassword">重置密码</a><a href="javascript:void(0)" style="color:#009feb;padding-left: 8px;" lay-event="edit">修改</a><a href="javascript:void(0)" style="color:#009feb;padding-left: 8px;" lay-event="del">删除</a>';
                    }
                }
            ]],
            tools: {
                resetPassword: function (rowData) {
                    layuiFunciton.alert({
                        types: 'confirm', title: '重置密码为“666666”？', success: function () {
                            layuiFunciton.ajaxRequest({
                                url: layuiFunciton.URL + '/sysUser/updateSysUser',
                                method: 'post',
                                data: {id: rowData.id, password: 666666},
                                success: function (data) {
                                    if (data > 0) {
                                        layuiFunciton.success('操作成功！', function () {
                                            layuiFunciton.refresh('searchFrom', 'sysUserId');
                                        })
                                    } else {
                                        layuiFunciton.warn('操作失败！', function () {
                                            layuiFunciton.refresh('searchFrom', 'sysUserId');
                                        })
                                    }
                                }
                            });
                        }
                    });
                },
                add: function (rowData) {
                    sysRoleList = {};
                    layuiFunciton.layerOpen({
                        title: "添加", finder: "form", areaType: true, content: "add.html", end: function () {
                            layuiFunciton.refresh('searchFrom', 'sysUserId');
                        }
                    });
                },
                edit: function (rowData) {
                    sysRoleList = {};
                    $(rowData.sysRoleList).each(function (index, obj) {
                        sysRoleList[obj.id] = obj;
                    });
                    //查询用户权限
                    /*layuiFunciton.ajaxRequest({
                     url: layuiFunciton.URL + '/sysUser/findAutorityAll',
                     method: 'get',
                     data: {userId: rowData.id},
                     success: function (returnData) {
                     rowData.menuList = returnData;
                     }
                     });*/
                    layuiFunciton.layerOpen({
                        title: "修改",
                        finder: "form",
                        content: "edit.html",
                        areaType: true,
                        data: rowData,
                        end: function () {
                            layuiFunciton.refresh('searchFrom', 'sysUserId');
                        }
                    });
                }
                , del: function (rowData) {
                    layuiFunciton.alert({
                        types: 'confirm', title: '是否删除？', success: function () {
                            layuiFunciton.ajaxRequest({
                                url: layuiFunciton.URL + '/sysUser/updateSysUser',
                                method: 'post',
                                data: {id: rowData.id, delFlag: 1},
                                success: function (data) {
                                    if (data > 0) {
                                        layuiFunciton.success('操作成功！', function () {
                                            layuiFunciton.refresh('searchFrom', 'sysUserId');
                                        })
                                    } else {
                                        layuiFunciton.warn('操作失败！', function () {
                                            layuiFunciton.refresh('searchFrom', 'sysUserId');
                                        })
                                    }
                                }
                            });
                        }
                    });
                }
            },
            filter: [{
                types: 'form',   //   注：仅支持 from 、table 两种类型  监听 table的 tools 请使用 tools 属性
                filterTypes: 'switch',   //
                filterName: 'enabledDemo',
                returnData: function (obj) {
                    layuiFunciton.ajaxRequest({
                        url: layuiFunciton.URL + '/sysUser/updateAutority',
                        method: 'post',
                        data: {id: obj.value, enabled: obj.elem.checked ? 1 : 0, numberPasswordError: 0},
                        success: function (data) {
                            layuiFunciton.refresh('sysUserSearchForm', 'sysUserId');
                        }
                    });
                }
            }]
        });
    };
    //事件绑定
    this.listening = function () {
        $("#search_btn").click(function () {
            layuiFunciton.refresh('sysUserSearchForm', 'sysUserId', 1);
        });
    };
};
$(function () {
    sysUserList = new SysUserList();
    sysUserList.init();
    sysUserList.listening();
});